package com.comdo.newup_loan;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.comdo.newup_loan.base.BaseActivity;
import com.comdo.newup_loan.base.BasePresenter;
import com.comdo.newup_loan.comom.Config;
import com.comdo.newup_loan.model.LoginInfo;
import com.comdo.newup_loan.module.home.HomeFragment;
import com.comdo.newup_loan.module.login.LoginActivity;
import com.comdo.newup_loan.module.mine.MineFragment;
import com.comdo.newup_loan.module.repayment.RePaymentFragment;
import com.comdo.newup_loan.util.ACache;

import butterknife.BindView;

public class MainActivity extends BaseActivity implements BottomNavigationBar.OnTabSelectedListener {

    @BindView(R.id.bottom_navigation_bar)
    BottomNavigationBar mBottomNavigationBar;
    private FragmentManager fManager;
    private HomeFragment homeFragment;
    private RePaymentFragment rePaymentFragment;
    private MineFragment mineFragment;
    @Override
    protected void initView(Bundle savedInstanceState) {
        MyApplication.getInstance().addActivity(this);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        InitNavigationBar();
        getLoginInfo();
    }
    private void getLoginInfo(){
        Config.loginInfo=(LoginInfo) (ACache.get(MainActivity.this).getAsObject("loginInfo"));

    }
    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_main;
    }
    private void InitNavigationBar() {

        mBottomNavigationBar.setTabSelectedListener(this);
        mBottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        mBottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.tab_main_home, "主页"))
                .addItem(new BottomNavigationItem(R.drawable.tab_main_repay, "还款"))
                .addItem(new BottomNavigationItem(R.drawable.tab_main_mine, "我的"))
                .setFirstSelectedPosition(0)
                .initialise();
        if (fManager == null) {
            fManager = getSupportFragmentManager();
        }
        FragmentTransaction fTransaction = fManager.beginTransaction();
        if (homeFragment == null) {
            homeFragment = new HomeFragment();
            fTransaction.add(R.id.layFrame, homeFragment);
        } else {
            fTransaction.show(homeFragment);
        }
        fTransaction.commitAllowingStateLoss();
    }
    @Override
    public void onTabSelected(int position) {
        if (fManager == null) {
            fManager = getSupportFragmentManager();
        }
        FragmentTransaction fTransaction = fManager.beginTransaction();
        switch (position) {
            case 0:
                hideAllFragment(fTransaction);
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                    fTransaction.add(R.id.layFrame, homeFragment);
                } else {
                    fTransaction.show(homeFragment);
                }
                break;
            case 1:
                if (Config.loginInfo == null){
                    toLoginActivity();
                    finish();
                    return;
                }
                hideAllFragment(fTransaction);
                if (rePaymentFragment == null) {
                    rePaymentFragment = new RePaymentFragment();
                    fTransaction.add(R.id.layFrame, rePaymentFragment);
                } else {
                    fTransaction.show(rePaymentFragment);
                }
                break;
            case 2:
                if (Config.loginInfo == null){
                    toLoginActivity();
                    finish();
                    return;
                }
                hideAllFragment(fTransaction);
                if (mineFragment == null) {
                    mineFragment = new MineFragment();
                    fTransaction.add(R.id.layFrame, mineFragment);
                } else {
                    fTransaction.show(mineFragment);
                }
                break;
        }
        fTransaction.commitAllowingStateLoss();
    }
    private void toLoginActivity() {
        Intent intent=new Intent(MainActivity.this, LoginActivity.class);
        startActivity(intent);
    }
    @Override
    public void onTabUnselected(int position) {
        Log.d("onTabUnselected", "onTabUnselected: " + position);
    }

    @Override
    public void onTabReselected(int position) {
        Log.d("onTabReselected", "onTabReselected: " + position);
    }
    // 隐藏所有Fragment
    private void hideAllFragment(FragmentTransaction fragmentTransaction) {
        if (homeFragment != null)
            fragmentTransaction.hide(homeFragment);
        if (rePaymentFragment != null)
            fragmentTransaction.hide(rePaymentFragment);
        if (mineFragment != null)
            fragmentTransaction.hide(mineFragment);
    }

    @Override
    protected boolean translucentStatusBar() {
        return true;
    }

    @Override
    public BasePresenter getmPresenter() {
        return null;
    }

    @Override
    public void presenterSetView() {

    }
    private long exitTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT)
                        .show();
                exitTime = System.currentTimeMillis();
            } else {
                try {
                    System.exit(0);

                } catch (Exception e) {
                    System.exit(0);

                }
            }
        }
        return true;
    }
}